package Q_Learning;

public class GridWorld {
        public static final int xNum = 4;
        public static final int yNum = 4;
        
        int[][] Unit; 
        // new a 4*4 Grid
        public GridWorld(){
                Unit = new int[xNum][yNum]; 
        }
        // generate grid of static
        public void generate(){
                // initial
                for(int counter = 0 ; counter < xNum*yNum ; counter++){
                        Unit[counter/4][counter%4] = 0;
                }
                // change reward
                Unit[3][3] = 1;
                Unit[2][0] = -1;
        }
        // generate grid of random
        public void generate(int seed){
            if(seed<0 || seed>8){
            	System.out.println("Error Number of Rewards!");
            } else {
                // initial
                for(int counter = 0 ; counter < xNum*yNum ; counter++){
                        Unit[counter/4][counter%4] = 0;
                }
                // change reward
                int rp = Math.round((float)Math.random()*xNum*yNum);
                int rn = Math.round((float)Math.random()*xNum*yNum);
                for(int counter = 0 ; counter < seed ; counter++){
                	// detect
                	if(Unit[rp/4][rp%4] == 1 || Unit[rn/4][rn%4] == -1){
                		counter--;
                	} else {
                		Unit[rp/4][rp%4] = 1;
                		Unit[rn/4][rn%4] = -1;
                	}
              	}
            }
        }
        public int getReward(int x, int y, int i){
                return Unit[x][y];
        }
        public int printGrid(int x, int y){
        	System.out.println("( "+ x +" , "+ y +" ) = "+ Unit[x][y]);
        	return 0;
        }
        public int printAllGrid(){
        	for(int counter = 0 ; counter < xNum*yNum ; counter++){
        		System.out.println("( "+ counter/4 +" , "+ counter%4 +" ) = "+ Unit[counter/4][counter%4]);
        	}
        	return 0;
        } 
}
